var eap_manager_event = {
	init: function() {
		$('[name="model.contacter.id"]', getActiveDom()).closest('.jcombobox').data('beforeRequest', function(req) {
				req.filterProperty = 'parent.id';
				req.filter = $('[name="model.party.id"]', getActiveDom()).val();
				if (req.filter == '') {
					req.cancel = true;
				} 
				return req.pageNo + ',' + req.term + ',' + req.filter;
		});
		$('[name="model.bank.id"]', getActiveDom()).closest('.jcombobox').data('beforeRequest', function(req) {
				req.filterProperty = 'parent.id';
				req.filter = $('[name="model.company.id"]', getActiveDom()).val();
				if (req.filter == '') {
					req.cancel = true;
				} 
				return req.pageNo + ',' + req.term + ',' + req.filter;
		});
		
		$('[name="model.partyBank.id"]', getActiveDom()).closest('.jcombobox').data('beforeRequest', function(req) {
				req.filterProperty = 'parent.id';
				req.filter = $('[name="model.party.id"]', getActiveDom()).val();
				if (req.filter == '') {
					req.cancel = true;
				} 
				return req.pageNo + ',' + req.term + ',' + req.filter;
		});
		
		var path = getActiveDom().find('.jform').eq(0).attr('action');
		if (path.indexOf('purchaseOrder') > 0) {
			$('[name="model.billTo.id"]', getActiveDom()).closest('.jcombobox').data('beforeRequest', function(req) {
					req.filterProperty = 'parent.id';
					req.filter = $('[name="model.company.id"]', getActiveDom()).val();
					if (req.filter == '') {
						req.cancel = true;
					} 
					return req.pageNo + ',' + req.term + ',' + req.filter;
				});
	
			$('[name="model.shipTo.id"]', getActiveDom()).closest('.jcombobox').data('beforeRequest', function(req) {
					req.filterProperty = 'parent.id';
					req.filter = $('[name="model.company.id"]', getActiveDom()).val();
					if (req.filter == '') {
						req.cancel = true;
					} 
					return req.pageNo + ',' + req.term + ',' + req.filter;
				});			
		
		} else {
			$('[name="model.billTo.id"]', getActiveDom()).closest('.jcombobox').data('beforeRequest', function(req) {
					req.filterProperty = 'parent.id';
					req.filter = $('[name="model.party.id"]', getActiveDom()).val();
					if (req.filter == '') {
						req.cancel = true;
					} 
					return req.pageNo + ',' + req.term + ',' + req.filter;
				});
	
			$('[name="model.shipTo.id"]', getActiveDom()).closest('.jcombobox').data('beforeRequest', function(req) {
					req.filterProperty = 'parent.id';
					req.filter = $('[name="model.party.id"]', getActiveDom()).val();
					if (req.filter == '') {
						req.cancel = true;
					} 
					return req.pageNo + ',' + req.term + ',' + req.filter;
				});			
		
		}
		
		
		$('[cmbname="model.party.id"]', getActiveDom()).on('blur', function() {
			// show the /sale/customer.getAccountInfo
			var $this = $(this);
			var partyId = $this.prev('[name="model.party.id"]').val();
			var oldVal = $this.data('oldVal');
			if (partyId != '' && partyId != oldVal) {
				$this.data('oldVal', partyId);
				var url = g_base_site + '/espirit/sale/customer.getAccountDefaultInfo.html?_resultCode=$json&target=execute'
					+ '&properties=currency.id,currency.name,taxRate.id,taxRate.code,paymentTerm.id,paymentTerm.name,paymentMode.id,paymentMode.name,deliveryTerm.id,deliveryTerm.name,deliveryMode.id,deliveryMode.name,defaultContacter.id,defaultContacter.name,defaultBank.id,defaultBank.name,defaultShipTo.id,defaultShipTo.address,defaultBillTo.id,defaultBillTo.address&model.id=' + partyId;
				var $tr = $this.closest('tr');
				$.getJSON(url, function(jsonData) {
					$('[name="model.contacter.id"]', getActiveDom()).val(jsonData['defaultContacter'].id).next('[cmbname]').val(jsonData['defaultContacter'].name);
					$('[name="model.partyBank.id"]', getActiveDom()).val(jsonData['defaultBank'].id).next('[cmbname]').val(jsonData['defaultBank'].name);
					$('[name="model.currency.id"]', getActiveDom()).val(jsonData.currency.id).next('[cmbname]').val(jsonData.currency.name);
					$('[name="model.taxRate.id"]', getActiveDom()).val(jsonData['taxRate'].id).next('[cmbname]').val(jsonData['taxRate'].code);
					$('[name="model.paymentTerm.id"]', getActiveDom()).val(jsonData.paymentTerm.id).next('[cmbname]').val(jsonData.paymentTerm.name);
					$('[name="model.paymentMode.id"]', getActiveDom()).val(jsonData.paymentMode.id).next('[cmbname]').val(jsonData.paymentMode.name);
					$('[name="model.deliveryMode.id"]', getActiveDom()).val(jsonData.deliveryMode.id).next('[cmbname]').val(jsonData.deliveryMode.name);
					$('[name="model.deliveryTerm.id"]', getActiveDom()).val(jsonData.deliveryTerm.id).next('[cmbname]').val(jsonData.deliveryTerm.name);
					if (path.indexOf('salesOrder') > 0){
						$('[name="model.billTo.id"]', getActiveDom()).val(jsonData.defaultBillTo.id).next('[cmbname]').val(jsonData.defaultBillTo.address);
						$('[name="model.shipTo.id"]', getActiveDom()).val(jsonData.defaultShipTo.id).next('[cmbname]').val(jsonData.defaultShipTo.address);
					}
				});
			}
		});
		
		
		$('[cmbname="model.company.id"]', getActiveDom()).on('blur', function() {
			var $this = $(this);
			var partyId = $this.prev('[name="model.company.id"]').val();
			var oldVal = $this.data('oldVal');
			if (partyId != '' && partyId != oldVal) {
				$this.data('oldVal', partyId);
				var url = g_base_site + '/espirit/sale/customer.getAccountDefaultInfo.html?_resultCode=$json&target=execute'
					+ '&properties=defaultContacter.id,defaultContacter.name,defaultBank.id,defaultBank.name,defaultShipTo.id,defaultShipTo.address,defaultBillTo.id,defaultBillTo.address&model.id=' + partyId;
				var $tr = $this.closest('tr');
				$.getJSON(url, function(jsonData) {
					$('[name="model.bank.id"]', getActiveDom()).val(jsonData['defaultBank'].id).next('[cmbname]').val(jsonData['defaultBank'].name);
					if (path.indexOf('purchaseOrder') > 0){
						$('[name="model.billTo.id"]', getActiveDom()).val(jsonData.defaultBillTo.id).next('[cmbname]').val(jsonData.defaultBillTo.address);
						$('[name="model.shipTo.id"]', getActiveDom()).val(jsonData.defaultShipTo.id).next('[cmbname]').val(jsonData.defaultShipTo.address);
					}
				});
			}
		});
		
		var $priceDependObjs = $('[cmbname="model.taxRate.id"],[cmbname="model.currency.id"],[cmbname="model.importRate.id"],[cmbname="model.serviceRate.id"]', getActiveDom());
		$priceDependObjs.on('change', function() {
			// generate an event to force the price is updated
			if ($(this).val() != '') {			
				$('[name=qty]', getActiveDom()).each(function() {
					calculateTotalPrice(this, true);
				});
			}
		});
	
		
		
		getActiveDom().on('change', '[name=qty]', function() {
			calculateTotalPrice(this);
		});
		getActiveDom().on('change', '[_name=grossPrice],[_name=price]', function() {
			calculateTotalPrice(this);
		});
		
		getActiveDom().on('blur', '[cmbname=intPartNumber]', function() {
			var $this = $(this);
			var partNumber = $this.val();
			var oldVal = $this.data('oldVal');
			if (partNumber != '' && partNumber != oldVal) {
				$this.data('oldVal', partNumber);
				var url = g_base_site + '/espirit/eap/manager/partItem.getPartItemByPartNumber.html?_resultCode=$json&target=execute'
					+ '&properties=brand.id,brand.name,eccn.id,eccn.code&partNumber=' + encodeURIComponent(partNumber);
				var $tr = $this.closest('tr');
				$.getJSON(url, function(jsonData) {
					$tr.find('[name="brand.id"]').val(jsonData.brand.id).next('[cmbname]').val(jsonData.brand.name);
					$tr.find('[name="eccn.id"]').val(jsonData.eccn.id).next('[cmbname]').val(jsonData.eccn.code);
				});
			}
		});
		
		
		$('[name="model.freight"]', getActiveDom()).on('blur', function() {
			calculateSharePrice();
		});
		
		getActiveDom().on('blur', '[cmbname="partItem.id"]', function() {
			var $this = $(this);
			var partNumber = $this.val();
			var oldVal = $this.data('oldVal');
			if (partNumber != '' && partNumber != oldVal) {
				$this.data('oldVal', partNumber);
				var url = g_base_site + '/espirit/eap/manager/partItem.getPartItemByPartNumber.html?_resultCode=$json&target=execute'
					+ '&properties=brand.id,brand.name,mfrNumber&partNumber=' + encodeURIComponent(partNumber);
				var $tr = $this.closest('tr');
				$.getJSON(url, function(jsonData) {
					$tr.find('[txtname="partItem.brand.id"]').html(jsonData.brand.name);
					$tr.find('[txtname="partItem.mfrNumber"]').html(jsonData.mfrNumber);
					$tr.find('[name="partItem.mfrNumber"]').val(jsonData.mfrNumber).next('[cmbname]').val(jsonData.mfrNumber);
				});
			}
		});
		
		getActiveDom().on('blur', '[cmbname="partItem.mfrNumber"]', function() {
			var $this = $(this);
			var mfrNumber = $this.val();
			var oldVal = $this.data('oldVal');
			var $tr = $this.closest('tr');
			if (mfrNumber != '' && mfrNumber != oldVal) {
				$this.data('oldVal', mfrNumber);
				var url;
				var path = getActiveDom().find('.jform').eq(0).attr('action');
				if (path.indexOf('salesOrderItem.generatePL') > 0 || path.indexOf('packingList') > 0) {
					var warehouseId = $tr.find('[name="warehouse.id"]').val();
					url = g_base_site + '/espirit/eap/manager/partItem.getPartItemInfo.html?_resultCode=$json&target=execute'
						+ '&properties=brand.id,brand.name,partNumber,id,jitQty&mfrNumber=' + encodeURIComponent(mfrNumber)+'&warehouse.id=' + warehouseId;
				} else {
					url = g_base_site + '/espirit/eap/manager/partItem.getPartItemByMfrNumber.html?_resultCode=$json&target=execute'
						+ '&properties=brand.id,brand.name,partNumber,id&mfrNumber=' + encodeURIComponent(mfrNumber);
				}
				$.getJSON(url, function(jsonData) {
					$tr.find('[txtname="partItem.brand.id"]').html(jsonData.brand.name);
					$tr.find('[name="partItem.id"]').val(jsonData.id).next('[cmbname]').val(jsonData.partNumber);
					$tr.find('[txtname="brandName"]').html(jsonData.brand.name);
					$tr.find('[txtname="partNumber"]').html(jsonData.partNumber);
					$tr.find('[name="partNumber"]').val(jsonData.partNumber).next('[cmbname]').val(jsonData.partNumber);
					$tr.find('[name="brandName"]').val(jsonData.brand.name).next('[cmbname]').val(jsonData.brand.name);
					$tr.find('[txtname="inventory.qty"]').html(jsonData.jitQty);
				});
			}
		});
		
		
		getActiveDom().on('blur', '[cmbname="warehouse.id"]', function() {
			var path = getActiveDom().find('.jform').eq(0).attr('action');
			if (path.indexOf('salesOrderItem.generatePL') > 0 || path.indexOf('packingList') > 0) {
				var $this = $(this);
				var $tr = $this.closest('tr');
				var warehouseId = $tr.find('[name="warehouse.id"]').val();
				var oldVal = $this.data('oldVal');
				var mfrNumber = $tr.find('[name="partItem.mfrNumber"]').val();
				if (warehouseId != '' && warehouseId != oldVal && mfrNumber != '') {
					$this.data('oldVal', warehouseId);
					var url = g_base_site + '/espirit/eap/manager/partItem.getPartItemInfo.html?_resultCode=$json&target=execute'
						+ '&properties=brand.id,brand.name,partNumber,id,jitQty&mfrNumber=' + encodeURIComponent(mfrNumber)+'&warehouse.id=' + warehouseId;
					$.getJSON(url, function(jsonData) {
						$tr.find('[txtname="partItem.brand.id"]').html(jsonData.brand.name);
						$tr.find('[name="partItem.id"]').val(jsonData.id).next('[cmbname]').val(jsonData.partNumber);
						$tr.find('[txtname="brandName"]').html(jsonData.brand.name);
						$tr.find('[txtname="partNumber"]').html(jsonData.partNumber);
						$tr.find('[name="partNumber"]').val(jsonData.partNumber).next('[cmbname]').val(jsonData.partNumber);
						$tr.find('[name="brandName"]').val(jsonData.brand.name).next('[cmbname]').val(jsonData.brand.name);
						$tr.find('[txtname="inventory.qty"]').html(jsonData.jitQty);
					});						
				}
			}
		});
	}
}